{ "cells": [ { "cell_type": "code", "execution_count": 1, "id": "several-colorado", "metadata": {}, "outputs": [], "source": [ "# This script calls the module 'modAdv.py' which contains the function \n", "# 'primes_adv()' to find all of the prime numbers between 'a'\n", "# and 'b'. It's an \"advanced\" version of the function 'primes()'. There's\n", "# nothing different about how we're calling the function. It's just that\n", "# what's executed within the actual function is slightly more sophisticated\n", "# than what's in 'primes()'.\n", "\n", "# 'primes_adv()' maintains a file of the sequential prime numbers that it\n", "# has previously found. Then, if a user asks for prime numbers that it has\n", "# previously found, it just consults the list and tells the user the\n", "# numbers they want to know without doing any searching. Only if the user\n", "# has asked for prime numbers that it has never found before, does it\n", "# actually implement a search.\n", "\n", "# In addition, 'primes_adv()' uses this list of prime numbers to\n", "# intelligently search for new prime numbers. To check if n is a prime\n", "# number, it is only necessary to check if it is a factor of any of the prime\n", "# numbers less than n. For example, we don't need to check if 27 is a\n", "# factor of n. If it is, then 3 is also a factor of n and we would have\n", "# already determined that n is not prime. By eliminating as many factors\n", "# from our search as possible, we can greatly enhance the efficiency of our\n", "# search for new prime numbers.\n", "\n", "# As of Sept. 28, 2020, all of the prime number between 1 and about 1.317e9 \n", "# have been found and sequentially listed by 'primes_adv()'. So far, the \n", "# function has sequentially listed 66,051,364 prime numbers.\n", "\n", "# Note that this is no where close to the largest prime number ever found.\n", "# As I type this, the largest know prime number is 2^(57,885,161) - 1. A\n", "# number that is a ridiculous 17,425,170 digits long. However, people do\n", "# not know the sequential list of primes up to this number.\n", "\n", "# Note that, after creating this script, I read that people use much faster\n", "# sieve programs to find sequential prime numbers.\n", "\n", "# The function that this script calls is named 'primes_adv(a, b)'. It finds all\n", "# of the prime numbers between input integers a and b. The code saved in\n", "# 'modAdv.py' is shown below without any explanations. If you open the\n", "# file 'modAdv.py', you will find some additional comments that help\n", "# explain the purpose of the various lines of code.\n", "\n", "# To run this function, you will need to have the files 'modAdv.py',\n", "# 'primes_max.txt', and 'primes_list.txt' in the same directory as the\n", "# script that calls 'primes_adv()'. 'primes_list.txt' is inside\n", "# 'primes_list.zip' which can be downloaded from my website. With a list\n", "# all prime numbers less than 1.317e9. When unzipped, 'primes_list.txt' is \n", "# about 650 Mb.\n", "\n", "#def primes_adv(a, b):\n", "# piN = []\n", "# if a % 1 == 0 and b % 1 == 0 and a > 0 and b > 0 and b > a:\n", "# import numpy as np \n", "# import pandas as pd\n", "# # with open('primes_list.txt', 'r') as f:\n", "# # lineList = f.readlines()\n", "# # with open(\"primes_list.txt\", \"ab\") as f:\n", "# # if len(lineList[-1].split('\\n')) == 1:\n", "# # f.write(b\"\\n\")\n", "# primeMax = int(np.loadtxt(\"primes_max.txt\"))\n", "# primeNums = pd.read_csv(\"./primes_list.txt\", delimiter=\"\\n\", header = None).values[:, 0]\n", "# if b <= primeMax:\n", "# i = 0\n", "# while i <= len(primeNums) - 1 and primeNums[i] <= b:\n", "# if primeNums[i] >= a:\n", "# piN = piN + [primeNums[i]]\n", "# i += 1\n", "# f = np.array(piN) \n", "# n = len(f) \n", "# elif b > primeMax:\n", "# for i in range(primeMax + 1, int(b) + 1):\n", "# cnt = 0\n", "# if i != 1 and i % 2 == 1 and sum(map(int, str(i))) % 3 != 0\\\n", "# and i % 10 != 5 or i == 2 or i == 3 or i == 5:\n", "# j = 3\n", "# while primeNums[j] < i/primeNums[j - 1] and cnt == 0:\n", "# if i % primeNums[j] == 0: \n", "# cnt = 1\n", "# primeMax = i\n", "# j += 1\n", "# if cnt == 0:\n", "# primeMax = i\n", "# primeNums = np.append(primeNums, i)\n", "# np.savetxt('primes_max.txt', np.array([primeMax]), fmt='%i', delimiter='\\n')\n", "# with open(\"primes_list.txt\", \"ab\") as f:\n", "# np.savetxt(f, np.array([i]), fmt='%i')\n", "# else:\n", "# primeMax = i\n", "# # np.savetxt('primes_max.txt', np.array([primeMax]), fmt='%i', delimiter='\\n')\n", "# # We could update primes_max.txt, but I commented out the line above to save time (i.e. not necessary to constantly update primes_max.txt.\n", "# np.savetxt('primes_max.txt', np.array([primeMax]), fmt='%i', delimiter='\\n')\n", "# i = 0\n", "# while i <= len(primeNums) - 1 and primeNums[i] <= b:\n", "# if primeNums[i] >= a:\n", "# piN = piN + [primeNums[i]]\n", "# i += 1\n", "# f = np.array(piN) \n", "# n = len(f) \n", "# else:\n", "# f = 'ERROR: a and b in primes(a, b) must be positive integers with b > a.'\n", "# n = -1 \n", "# return f, n " ] }, { "cell_type": "code", "execution_count": 4, "id": "communist-demographic", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2021-03-11 09:10:40.475231\n", "2021-03-11 09:10:49.997399\n", "A list of the 594 sequential prime numbers between 20000000 and 20010000\n" ] }, { "data": { "text/plain": [ "array([20000003, 20000023, 20000033, 20000047, 20000059, 20000063,\n", " 20000069, 20000077, 20000081, 20000093, 20000107, 20000147,\n", " 20000153, 20000159, 20000161, 20000171, 20000213, 20000221,\n", " 20000243, 20000269, 20000287, 20000297, 20000303, 20000311,\n", " 20000327, 20000329, 20000339, 20000353, 20000359, 20000377,\n", " 20000389, 20000429, 20000443, 20000471, 20000503, 20000507,\n", " 20000531, 20000537, 20000543, 20000567, 20000569, 20000573,\n", " 20000599, 20000621, 20000623, 20000641, 20000671, 20000689,\n", " 20000693, 20000707, 20000713, 20000723, 20000753, 20000779,\n", " 20000791, 20000801, 20000821, 20000837, 20000839, 20000843,\n", " 20000861, 20000867, 20000873, 20000879, 20000909, 20000917,\n", " 20000933, 20000951, 20000969, 20000971, 20001001, 20001019,\n", " 20001029, 20001031, 20001067, 20001073, 20001083, 20001139,\n", " 20001151, 20001161, 20001181, 20001203, 20001217, 20001227,\n", " 20001239, 20001253, 20001259, 20001263, 20001269, 20001277,\n", " 20001301, 20001307, 20001341, 20001361, 20001389, 20001439,\n", " 20001451, 20001491, 20001517, 20001521, 20001551, 20001557,\n", " 20001613, 20001659, 20001679, 20001703, 20001763, 20001769,\n", " 20001797, 20001799, 20001811, 20001823, 20001833, 20001841,\n", " 20001847, 20001853, 20001899, 20001901, 20001929, 20001937,\n", " 20001959, 20001977, 20001983, 20002007, 20002027, 20002061,\n", " 20002067, 20002097, 20002123, 20002133, 20002159, 20002187,\n", " 20002189, 20002201, 20002261, 20002313, 20002321, 20002331,\n", " 20002337, 20002361, 20002363, 20002369, 20002397, 20002399,\n", " 20002417, 20002429, 20002453, 20002457, 20002487, 20002529,\n", " 20002531, 20002537, 20002553, 20002579, 20002603, 20002613,\n", " 20002639, 20002651, 20002667, 20002669, 20002673, 20002679,\n", " 20002691, 20002729, 20002733, 20002739, 20002757, 20002771,\n", " 20002799, 20002823, 20002831, 20002841, 20002907, 20002919,\n", " 20002933, 20002951, 20002979, 20002981, 20003003, 20003023,\n", " 20003047, 20003057, 20003063, 20003089, 20003107, 20003117,\n", " 20003131, 20003149, 20003161, 20003167, 20003171, 20003173,\n", " 20003177, 20003201, 20003227, 20003231, 20003267, 20003287,\n", " 20003293, 20003297, 20003317, 20003369, 20003383, 20003393,\n", " 20003443, 20003461, 20003497, 20003513, 20003521, 20003549,\n", " 20003563, 20003567, 20003597, 20003609, 20003663, 20003671,\n", " 20003677, 20003681, 20003693, 20003699, 20003701, 20003719,\n", " 20003801, 20003803, 20003821, 20003833, 20003857, 20003861,\n", " 20003869, 20003887, 20003899, 20003909, 20003923, 20003927,\n", " 20003933, 20003957, 20003969, 20003981, 20004001, 20004011,\n", " 20004031, 20004041, 20004059, 20004071, 20004107, 20004133,\n", " 20004137, 20004139, 20004191, 20004199, 20004211, 20004217,\n", " 20004221, 20004233, 20004239, 20004253, 20004289, 20004307,\n", " 20004311, 20004323, 20004343, 20004367, 20004419, 20004431,\n", " 20004451, 20004487, 20004497, 20004521, 20004527, 20004533,\n", " 20004541, 20004559, 20004581, 20004583, 20004667, 20004683,\n", " 20004689, 20004709, 20004713, 20004727, 20004731, 20004749,\n", " 20004773, 20004811, 20004821, 20004823, 20004833, 20004851,\n", " 20004883, 20004893, 20004923, 20004931, 20004947, 20004973,\n", " 20004977, 20004979, 20004997, 20005003, 20005021, 20005087,\n", " 20005093, 20005129, 20005159, 20005163, 20005177, 20005207,\n", " 20005213, 20005231, 20005259, 20005289, 20005291, 20005301,\n", " 20005313, 20005319, 20005343, 20005351, 20005357, 20005361,\n", " 20005367, 20005369, 20005393, 20005397, 20005429, 20005439,\n", " 20005441, 20005451, 20005459, 20005483, 20005501, 20005507,\n", " 20005511, 20005519, 20005523, 20005529, 20005541, 20005589,\n", " 20005603, 20005637, 20005649, 20005673, 20005681, 20005693,\n", " 20005709, 20005717, 20005721, 20005729, 20005763, 20005807,\n", " 20005823, 20005829, 20005847, 20005849, 20005861, 20005871,\n", " 20005873, 20005877, 20005891, 20005903, 20005907, 20005919,\n", " 20005949, 20005981, 20006003, 20006023, 20006047, 20006053,\n", " 20006057, 20006083, 20006087, 20006101, 20006123, 20006149,\n", " 20006177, 20006179, 20006209, 20006219, 20006227, 20006243,\n", " 20006279, 20006339, 20006351, 20006359, 20006419, 20006423,\n", " 20006429, 20006431, 20006489, 20006507, 20006509, 20006531,\n", " 20006533, 20006543, 20006551, 20006561, 20006579, 20006611,\n", " 20006621, 20006641, 20006663, 20006681, 20006683, 20006717,\n", " 20006731, 20006737, 20006741, 20006743, 20006797, 20006801,\n", " 20006813, 20006827, 20006837, 20006843, 20006851, 20006869,\n", " 20006881, 20006887, 20006923, 20006927, 20006953, 20006957,\n", " 20006971, 20006983, 20006993, 20007017, 20007041, 20007049,\n", " 20007061, 20007107, 20007149, 20007157, 20007167, 20007173,\n", " 20007203, 20007223, 20007257, 20007259, 20007287, 20007293,\n", " 20007301, 20007311, 20007319, 20007359, 20007371, 20007373,\n", " 20007409, 20007457, 20007479, 20007503, 20007523, 20007527,\n", " 20007529, 20007541, 20007557, 20007569, 20007601, 20007643,\n", " 20007661, 20007671, 20007683, 20007719, 20007721, 20007737,\n", " 20007749, 20007773, 20007781, 20007791, 20007811, 20007847,\n", " 20007851, 20007859, 20007863, 20007881, 20007919, 20007971,\n", " 20007973, 20007979, 20008019, 20008039, 20008049, 20008073,\n", " 20008097, 20008127, 20008129, 20008141, 20008151, 20008169,\n", " 20008171, 20008187, 20008199, 20008207, 20008211, 20008217,\n", " 20008223, 20008243, 20008249, 20008253, 20008259, 20008271,\n", " 20008279, 20008291, 20008301, 20008319, 20008327, 20008333,\n", " 20008343, 20008381, 20008447, 20008477, 20008487, 20008523,\n", " 20008529, 20008531, 20008553, 20008559, 20008589, 20008603,\n", " 20008607, 20008621, 20008649, 20008657, 20008663, 20008669,\n", " 20008721, 20008759, 20008801, 20008817, 20008829, 20008871,\n", " 20008903, 20008907, 20008909, 20008913, 20008921, 20008939,\n", " 20008951, 20008991, 20008999, 20009029, 20009047, 20009053,\n", " 20009113, 20009117, 20009137, 20009173, 20009183, 20009243,\n", " 20009273, 20009279, 20009293, 20009329, 20009347, 20009371,\n", " 20009377, 20009383, 20009389, 20009393, 20009411, 20009419,\n", " 20009441, 20009443, 20009447, 20009453, 20009497, 20009513,\n", " 20009531, 20009533, 20009537, 20009543, 20009569, 20009581,\n", " 20009593, 20009597, 20009611, 20009651, 20009653, 20009659,\n", " 20009677, 20009713, 20009719, 20009741, 20009753, 20009761,\n", " 20009813, 20009819, 20009863, 20009887, 20009917, 20009923,\n", " 20009929, 20009939, 20009947, 20009981, 20009987, 20009999],\n", " dtype=int64)" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "from datetime import datetime\n", "print(datetime.now())\n", "n_min = 20e6\n", "n_max = 20.01e6;\n", "import modAdv\n", "xx, x = modAdv.primes_adv(n_min, n_max)\n", "print(datetime.now())\n", "print('A list of the', x, 'sequential prime numbers between', int(n_min),\\\n", " 'and', int(n_max))\n", "xx" ] }, { "cell_type": "code", "execution_count": null, "id": "adult-tolerance", "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.8" } }, "nbformat": 4, "nbformat_minor": 5 }